<?php

namespace app\admin\controller;

use app\extend\Backupandrestore;

class Backup extends Base
{
    protected function _infoModule()
    {
        return array(
            'info' => array(
                'name' => '备份管理',
                'description' => '管理网站后台管理员',
            ),
            'menu' => array(
                array(
                    'name' => '列表',
                    'url' => url('backup/index'),
                    'icon' => 'list',
                ),
                array(
                    'name' => '数据表',
                    'url' => url('backup/mysql'),
                    'icon' => 'eye',
                ),
            ),
            '_info' => array(

            ),
        );
    }

    private function configDB(){
        $mysqlConfig = config('mysql')['connections']['mysql'];
        $mysqlConfig['path'] = PUBLIC_PATH . "mysql/";		//存储路径
        return $mysqlConfig;
    }

    public function index()
    {
        $config = $this->configDB();

        $mysqlUrl = $config['path'];
        $FileArr = myScandir($mysqlUrl);
        $mysqlList = array();
        foreach ($FileArr as $i => $n){
            if($n != 'PHPMyAdminInitialData.sql' && $i>1){

                $FileSize = filesize($mysqlUrl . $n)/1024;
                if ($FileSize < 1024){
                    $FileSize = number_format($FileSize,2) . ' KB';
                } else {
                    $FileSize = '<font color="#FF0000">' . number_format($FileSize/1024,2) . '</font> MB';
                }

                $mysqlList[$i-2]['size'] = $FileSize;
                $mysqlList[$i-2]['time'] = date('Y-m-d H:i:s',filemtime($mysqlUrl.$n));
                $mysqlList[$i-2]['file'] = $n;
            }
        }
        $this->assign('list',$mysqlList);
        $this->assign('dbname',$config['database']);
        $this->assign('dbprefix',$config['prefix']);

        $this->fetch('index');
    }

    //备份
    public function back()
    {
        $config = $this->configDB();

        $backup = new Backupandrestore($config);
        $info = $backup->backup();

        jsonApi($info['code'], $info['msg']);
    }

    //还原
    public function restore()
    {
        $request = $this->request;
        $file = $request->post('file');

        $config = $this->configDB();
        $backup = new Backupandrestore($config);
        $info = $backup->restore($file);

        jsonApi($info['code'], $info['msg']);
    }

    //删除
    public function del()
    {
        $request = $this->request;
        $file = $request->post('file');

        $config = $this->configDB();
        $backup = new Backupandrestore($config);
        $info = $backup->delfilename($file);

        jsonApi(1, $info);
    }

    //下载
    public function download()
    {
        $request = $this->request;
        $file = $request->get('file');

        $config = $this->configDB();
        $backup = new Backupandrestore($config);
        $backup->downloadFile($file);
    }

    //表列表
    public function mysql()
    {
        $config = $this->configDB();
        $backup = new Backupandrestore($config);
        $list = $backup->get_tablename();
        $this->assign('list',$list);

        $this->fetch('mysql');
    }

    //表结构
    public function structure()
    {
        $request = $this->request;
        $tablename = $request->get('tablename');

        $config = $this->configDB();
        $backup = new Backupandrestore($config);
        $info = $backup->get_table_structure($tablename);
        $this->assign('info',$info);

        $this->fetch('structure');
    }

}
